﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int er(int x)//判断一个整数的二进制位数有多少个1
{
	int count = 0;//牛逼的代码，反正我真想不到
	while (x)
	{
		x = x & (x - 1);
		count++;
	}
	return count;
}

int re(int x)//迭代
{
	int count = 0;
	/*int a = 32;
	while (a--)
	{
		if (x == (x | (1 << a)))
		{
			count++;
		}
	}*/
	while (x)
	{
		if (x == (x | (1 << 31)))
		{
			count++;
		}
		x <<= 1;
	}
	return count;
}


int main()
{
	int a = 1023;
	printf("1023: %d\n", er(a));
	printf("1023: %d\n", re(a));

	a = -1;
	printf("-1  : %d\n", er(a));
	printf("-1  : %d\n", re(a));

	return 0;
}

